package com.example.qihuibao.widget.dialog

import android.view.View
import android.widget.*
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.*
import com.example.qihuibao.constant.Global
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.extension.toast
import com.example.qihuibao.widget.view.CommonRecyclerView
import com.example.qihuibao.widget.view.EdittextWithText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@author: cww
 *@desc:企业信息操作
 *@date: 5/16/23 10:58 AM
 */
class EmployInfoAddDialog() : BaseDialogFragment(R.layout.dialog_employ_info_add) {
    private var employTypeSelected = ""
    private var depId1Selected = ""
    private var statusIdSelected = ""

    override fun initView() {
        mView?.findViewById<View>(R.id.iv_close)?.setOnClickListener { dismiss() }
        mView?.findViewById<View>(R.id.btn_add)?.setOnClickListener { add() }
        lifecycleScope.launch {
            var empTypeList :List<EmpType>? = null
            var depList :List<Department>? = null
            withContext(Dispatchers.IO) {
                val bean = MyDatabase.db.enterpriseInfoDao().queryInId(Global.CorpID)
                if (bean?.EmpTypeList != null) {
                    empTypeList = bean.EmpTypeList
                }
            }
            if (empTypeList != null) {
                val array = arrayListOf<String>()
                for (item in empTypeList!!) {
                    array.add(item.EmpTypeName?:"")
                }
                employTypeSelected = empTypeList!![0].EmpTypeID!!
                val sp1 = mView?.findViewById<Spinner>(R.id.sp_employ_type)
                sp1?.adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, array)
                sp1?.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        employTypeSelected = empTypeList!![position].EmpTypeID!!
                    }

                    override fun onNothingSelected(parent: AdapterView<*>?) {
                        //
                    }
                }
            }
            withContext(Dispatchers.IO) {
                depList = MyDatabase.db.departmentInfoDao().queryAll()
            }
            if (!depList.isNullOrEmpty()) {
                val array = arrayListOf<String>()
                for (item in depList!!) {
                    array.add(item.DepName1?:"")
                }
                depId1Selected = depList!![0].DepID1
                val sp2 = mView?.findViewById<Spinner>(R.id.sp_dep_id1)
                sp2?.adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, array)
                sp2?.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        depId1Selected = depList!![position].DepID1!!
                    }

                    override fun onNothingSelected(parent: AdapterView<*>?) {
                        //
                    }
                }
            }
        }
        statusIdSelected = "1"
        val sp3 = mView?.findViewById<Spinner>(R.id.sp_status)
        sp3?.adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, listOf("正常", "挂失", "冻结"))
        sp3?.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                statusIdSelected = "${position+1}"
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                //
            }
        }
    }

    private fun add() {
        val empId:String = mView?.findViewById<EditText>(R.id.et_employ_id)?.text.toString()
        val empName:String = mView?.findViewById<EditText>(R.id.et_employ_name)?.text.toString()
        if (empId.isNullOrEmpty()) {
            activity?.toast("人员ID不能为空")
            return
        }
        if (empName.isNullOrEmpty()) {
            activity?.toast("人员姓名不能为空")
            return
        }
        if (employTypeSelected.isEmpty()) {
            activity?.toast("人员类型不能为空")
            return
        }
        if (depId1Selected.isEmpty()) {
            activity?.toast("一级部门不能为空")
            return
        }
        (activity as MainActivity).getMainViewModel().addEmployInfo(ReqAddEmployInfoBean(EmpID = empId, EmpName = empName, EmpType = employTypeSelected, DepID1 = depId1Selected, Status = statusIdSelected)).observe(this) {
            activity?.toast(if (it.RetCode == "0") "新增成功" else "新增失败")
            if (it.RetCode == "0") {
                dismiss()
            }
        }
    }

}